'use strict'

const Koa = require('koa');
let a = 0;
const app = new Koa();
//ctx应用上下文 包括request response next传入的下一个异步函数
app.use(async function (ctx, next) {
    a = 1;
    console.log(a);
    await next();

    a = 2;
    console.log(a);
    console.log(`${ctx.request.method} ${ctx.request.url}`);


})


app.use(async function (ctx, next) {
    a = 5;
    console.log(a);
    const start = new Date();
    console.log(start.getTime());
    await next();
    a = 8;
    console.log(a);

})


app.use(async function (ctx, next) {
    console.log('third async');
    a = 6;
    console.log(a);

    //首先处理下一个异步函数
    await next();
    a = 7;
    console.log(a);
    ctx.response.type = 'text/json';
    ctx.response.body = { name: 'liu', age: 33 };

});


app.listen(3000);
console.log('app is running..............');